/**
 * 
 */
package com.study.map;

import java.util.Iterator;
import java.util.LinkedList;

/**
 * 猴子选大王 n只猴子围坐成一个圈,按顺时针方向从1到n编号.然后从1号猴子开始沿顺时针方向从1开始报数,报到m的猴子出局,
 * 再从刚出局猴子的下一个位置重新开始报数,如此重复,直至剩下一个猴子,它就是大王.
 * 
 * @author Administrator
 * @date 2019年6月22日
 */
public class SelectKing {

	private LinkedList<Integer> list;

	public SelectKing(int num) {
		list = new LinkedList();
		for (int i = 0; i < 12; i++) {
			list.addLast(i + 1);
		}
	}

	public int selectKing(int count) {
		int i = 0;
		while (list.size() > 1) {
			Iterator<Integer> iterator = list.iterator();
			while (iterator.hasNext()) {
				Integer next = iterator.next();
				i++;
				if(i < count){
					System.out.println("count:" + next +","+ i);
					
				}else{
					System.out.println("out: " + next);
					iterator.remove();
					i=0;
				}
				
			}
		}
		
		
		return count;

	}

	public static void main(String[] args) {

		SelectKing selectKing = new SelectKing(12);
		selectKing.selectKing(2);
	}
}
